package com.rightsidetech.http.controller.admin.store;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.entity.ActGiftActivityStore;
import com.rightsidetech.activity.service.ActGiftService;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.store.ActAreaAgentStoreReq;
import com.rightsidetech.system.dtos.request.store.SysStoreInfoReq;
import com.rightsidetech.system.dtos.response.store.*;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.AgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import com.rightsidetech.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Package: com.rightsidetech.http.controller.admin.store
 * @ClassName: SysStoreInfoController
 * @Author: yinj
 * @CreateTime: 2021/10/20 15:13
 * @Description: 门店信息相关
 */
@Slf4j
@RestController
@RequestMapping("/admin/sys_store")
@ShenyuSpringMvcClient(path = "/admin/sys_store/**")
@Api(tags = "门店信息相关api")
public class SysStoreInfoController extends BaseController {

    @Resource
    private SysStoreInfoService sysStoreInfoService;
    @Resource
    private ActGiftService actGiftService;
    @Resource
    private AgentInfoService agentInfoService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("门店分页")
    public ResponseData<PageInfo<SysStoreInfoResp>> page(SysStoreInfoReq request){
        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long loginId = Long.parseLong(getAttribute());
        SysUserLoginInfo loginInfo = sysUserService.selectUserById(loginId);
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        if (loginInfo.getSysCompanyAreaId() != 0){
            request.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return sysStoreInfoService.page(request);

    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/find_details")
    @ApiOperation("详情")
    public ResponseData<SysStoreInfoResp> findDetails(Long id){
        return sysStoreInfoService.findDetails(id,null);
    }


    @Log
    @GetMapping("/search")
    @ApiOperation("总平台-门店下拉框列表")
    @Token
    public ResponseData<List<StoreSearchResp>> search(String content){

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        Long areaId = null;
        if(loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }

        return sysStoreInfoService.search(content,null,areaId);
    }

    @Log
    @GetMapping("/search_open")
    @ApiOperation("总平台-门店下拉框列表--（只查询开启状态大区）")
    @Token
    public ResponseData<List<StoreSearchResp>> searchOpen(String content){
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        Long areaId = null;
        if(loginInfo.getSysCompanyAreaId() != 0){
            areaId = loginInfo.getSysCompanyAreaId();
        }

        return sysStoreInfoService.searchOpen(content,null,areaId);
    }


    @Log
    @GetMapping("/act_store")
    @ApiOperation("活动关联门店")
    public ResponseData<List<ActAreaAgentStoreResp>> actStore(ActAreaAgentStoreReq req){
        List<ActGiftActivityStore> actGiftActivityStores = null;
        if(ObjectUtil.isNotEmpty(req.getGiftActivityId())){
            actGiftActivityStores = actGiftService.selectActStoreByGiftId(req.getGiftActivityId());
        }
        List<ActStoreResp> storeActResps = sysStoreInfoService.selectStoreActResp();
        if(CollUtil.isNotEmpty(actGiftActivityStores)){
            for (ActStoreResp storeActResp : storeActResps) {
                for (ActGiftActivityStore activityStore : actGiftActivityStores) {
                    if(storeActResp.getId().equals(activityStore.getStoreInfoId())){
                        storeActResp.setType((byte)1);
                    }
                }
            }
        }
        Map<Long, List<ActStoreResp>> collect = storeActResps.stream().collect(Collectors.groupingBy(ActStoreResp::getSysAgentInfoId));
        List<ActAgentResp> agentResps = agentInfoService.selectAgentList(req);
        for (ActAgentResp resp : agentResps) {
            List<ActStoreResp> agentResult = collect.get(resp.getId());
            resp.setChildren(agentResult);
        }
        agentResps.removeIf(agentResp -> CollUtil.isEmpty(agentResp.getChildren()));
        Map<Long, List<ActAgentResp>> collect1 = agentResps.stream().collect(Collectors.groupingBy(ActAgentResp::getAreaId));
        List<ActAreaAgentStoreResp> result = agentInfoService.selectAreaList(req);
        for (ActAreaAgentStoreResp resp : result) {
            List<ActAgentResp> agentResps1 = collect1.get(resp.getId());
            resp.setChildren(agentResps1);
        }
        result.removeIf(area -> CollUtil.isEmpty(area.getChildren()));
        return ResponseData.success(result);
    }




}
